﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using System.Threading.Tasks;
using SCS.Common.Models;

namespace SCS.DAL
{
    public class MembershipRepository : Repository<MembershipUser>, IMembershipRepository
    {
        public MembershipRepository(SCSContext _context)
            : base(_context)
        { }
        /// <summary>
        /// Get a user by username
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public MembershipUser GetUser(string username)
        {
            return DBSet
                .Include(x => x.MembershipRoles)
                .FirstOrDefault(name => name.userName.ToLower() == username.ToLower());
        }

        #region Teacher Section
        /// <summary>
        /// Get All Teacher User
        /// </summary>
        /// <returns>Teacher User</returns>
        public IList<MembershipUser> GetAllTeacherUser()
        {
            return DBSet.Where(m => m.Teachers.Count > 0).ToList();
        }

        /// <summary>
        /// Search Teacher User
        /// </summary>
        /// <param name="searchString">text search</param>
        /// <returns>List User</returns>
        public IList<MembershipUser> SearchTeacherUser(string searchString)
        {
            return DBSet.Where(m => (m.userName.Contains(searchString) || m.Teachers.FirstOrDefault().teacherName.Contains(searchString)) && m.Teachers.Count > 0).ToList();
        }

        public IList<MembershipUser> SearchAdminUser(string searchString)
        {
            return DBSet.Where(m => (m.userName.Contains(searchString) && m.MembershipRoles.FirstOrDefault().roleID == 1)).ToList();
        }
        #endregion
        #region Parents Section
        /// <summary>
        /// Get All Parents User
        /// </summary>
        /// <returns>Parents User</returns>
        public IList<MembershipUser> GetAllParentsUser()
        {
            return DBSet.Where(m => m.Parents.Count > 0).ToList();
        }
       
        /// <summary>
        /// Search Parents User
        /// </summary>
        /// <param name="searchString">text search</param>
        /// <returns>List User</returns>
        public IList<MembershipUser> SearchParentsUser(string searchString)
        {
            return DBSet.Where(m => (m.userName.Contains(searchString) || m.Parents.FirstOrDefault().parentName.Contains(searchString)) && m.Parents.Count > 0).ToList();
        }
        #endregion

        /// <summary>
        /// Search Members
        /// </summary>
        /// <param name="username">user name</param>
        /// <param name="amount">amount</param>
        /// <returns>List user</returns>
        public IList<MembershipUser> SearchMembers(string username, int amount)
        {
            return DBSet
                            .Where(x => x.userName.ToUpper().Contains(username.ToUpper()))
                            .OrderBy(x => x.userName)
                            .Take(amount)
                            .ToList();
        }

        /// <summary>
        /// Get User by Email
        /// </summary>
        /// <param name="email">email address</param>
        /// <returns>User</returns>
        public MembershipUser GetUserByEmail(string email)
        {
            return DBSet
                .Include(x => x.MembershipRoles)
                .FirstOrDefault(name => name.email == email);
        }

        /// <summary>
        /// Get User by ID
        /// </summary>
        /// <param name="id">id of user</param>
        /// <returns>User</returns>
        public IList<MembershipUser> GetUsersById(int id)
        {
            return DBSet
              .Where(x => x.userID == id)
              .ToList();
        }

        /// <summary>
        /// Get current User
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>Current User</returns>
        public MembershipUser GetCurrentUser(int id)
        {
            return DBSet
                .Include(x => x.MembershipRoles)
                .FirstOrDefault(x => x.userID == id);
        }

        /// <summary>
        /// Count Member
        /// </summary>
        /// <returns>Total of member</returns>
        public int MemberCount()
        {
            return DBSet.Count();
        }
    }
}